package com.old.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.old.entity.*;
import com.old.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 86166
 */
@Slf4j
@Api(tags = "后端管理系统")
@RestController
@RequestMapping("/admin/old")
public class LoginController {

    @Autowired
    ManagementService managementService;

    @Autowired
    OldManService oldManService;

    @Autowired
    UserService userService;

    @Autowired
    ActivityService activityService;

    @Autowired
    JoinService joinService;

    @Autowired
    OrganizationService organizationService;

    /**
     * 管理员注册接口
     * @param management
     * @return
     */
    @ApiOperation("管理员注册")
    @PostMapping("managementRegist")
    public Map<String, Object> managementRegist(@RequestBody Management management) {
        List<Management> list = managementService.list();
        HashMap<String, Object> map= new HashMap<>();
        for (Management li : list) {
            if (li.getMname().equals(management.getMname())) {
                map.put("m", "用户名已存在，请重新注册！o(╥﹏╥)o");
                return map;
            }
        }
        boolean b = managementService.save(management);
        if (b) {
            map.put("m", "注册成功！(*^▽^*)");
            return map;
        }else {
            map.put("m", "注册失败！o(╥﹏╥)o");
            return map;
        }

    }

    /**
     * 管理员登录接口
     * @return
     */
    @ApiOperation("管理员登录")
    @PostMapping("managementLogin")
    public ResponseEntity<Management> managementLogin(@RequestBody Management management) {

        List<Management> list = managementService.list();
        for (Management li : list) {
            if (li.getMname().equals(management.getMname()) && li.getPassword().equals(management.getPassword())) {
                return new ResponseEntity<>(li, HttpStatus.OK);
            }
        }
        return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
    }

    /**
     * 查看所有管理员
     * @return
     */
    @ApiOperation("管理员查询")
    @PostMapping("queryManagement")
    public List<Management> queryManagement() {

        List<Management> list = managementService.list();
        return list;
    }

    @ApiOperation("用户注册")
    @PostMapping("userRegist")
    public Map<String , Object> userRegist(@RequestBody User user) {
        List<User> list = userService.list();
        HashMap<String, Object> map= new HashMap<>();
        for (User li : list) {
            if (user.getUsername().equals(li.getUsername())) {
                map.put("mes", "用户名已存在，请重新注册！o(╥﹏╥)o");
                return map;
            }
        }
        boolean b = userService.save(user);
        if (b) {
            map.put("mes", "注册成功！(*^▽^*)");
            return map;
        }else {
            map.put("mes", "注册失败！o(╥﹏╥)o");
            return map;
        }

    }

    @ApiOperation("用户登录")
    @PostMapping("userLogin")
    public ResponseEntity<User> userLogin(@RequestBody User user) {

        List<User> list = userService.list();
        for (User li : list) {
            if (li.getUsername().equals(user.getUsername()) && li.getPassword().equals(user.getPassword())) {
                return new ResponseEntity<>(li, HttpStatus.OK);
            }
        }
        return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
    }


    @ApiOperation("修改管理员信息")
    @PostMapping("update")
    public Map<String , Object> update(@RequestBody Management management) {
        boolean b = managementService.updateById(management);
        HashMap<String, Object> map= new HashMap<>();
        if (b) {
            map.put("message2", "修改成功！(*^▽^*)");
            return map;
        }else {
            map.put("message2", "修改失败！o(╥﹏╥)o");
            return map;
        }
    }

    @ApiOperation("修改用户信息")
    @PostMapping("updateUser")
    public Map<String , Object> updateUser(@RequestBody User user) {
        boolean b = userService.updateById(user);
        HashMap<String, Object> map= new HashMap<>();
        if (b) {
            map.put("msg", "修改成功！(*^▽^*)");
            return map;
        }else {
            map.put("msg", "修改失败！o(╥﹏╥)o");
            return map;
        }
    }

    @ApiOperation("显示所有老人信息")
    @GetMapping("showAll")
    public List<Oldman> showAll(){
        List<Oldman> list = oldManService.list();
        return list;
    }

    @ApiOperation("条件查询带分页")
    @PostMapping("findOldMan/{current}/{size}")
    public Page<Oldman> findOldMan(@PathVariable long current,
                                   @PathVariable long size,
                                   @RequestBody Oldman oldMan) {

        // 创建Page对象，传递当前页和每页记录数
        Page<Oldman> page = new Page<>(current, size);
        //构造条件
        QueryWrapper<Oldman> queryWrapper = new QueryWrapper<>();
        String name = oldMan.getName();
        int age = oldMan.getAge();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(age)) {
            queryWrapper.ge("age", age);
        }
        //调用方法实现分页查询
        Page<Oldman> setPage = oldManService.page(page, queryWrapper);

        return setPage;
    }

    @ApiOperation("添加老人信息")
    @PostMapping("addOldMan")
    public String addOldMan(@RequestBody Oldman oldMan){
        boolean save = oldManService.save(oldMan);
        if (save) {
            return "添加成功！";
        }
        else {
            return "添加失败！";
        }
    }

    @ApiOperation("删除老人信息")
    @DeleteMapping("deleteOldManById/{id}")
    public String deleteOldMan(@PathVariable int id){
        boolean b = oldManService.removeById(id);
        if (b) {
            return "添加成功！";
        }
        else {
            return "添加失败！";
        }
    }


    @ApiOperation("查看所有活动")
    @GetMapping("query/activity")
    public List<Activity> queryActivity(){

        List<Activity> list = activityService.list();

        return list;
    }

    @ApiOperation("发布活动")
    @PostMapping("public")
    public Map<String, Object> publish(@RequestBody Activity activity){

        boolean save = activityService.save(activity);
        HashMap<String, Object> map = new HashMap<>();
        if (save) {
            map.put("public", "发布成功！");
            return map;
        }
        else {
            map.put("public", "发布失败");
            return map;
        }

    }

    @ApiOperation("加入组织")
    @PostMapping("joinTeam")
    public Map<String, Object> joinTeam(@RequestBody User user){

        User user1 = userService.getById(user.getId());
        String name = user1.getTeamName();
        user1.setTeamName(user.getTeamName());
        boolean b = userService.updateById(user1);

        //构造条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("team_name", user.getTeamName());
        long count = userService.count(queryWrapper);

        //构造条件
        QueryWrapper<Organization> query = new QueryWrapper<>();
        query.eq("organization_name", user.getTeamName());
        Organization one = organizationService.getOne(query);
        one.setNumber(count);
        organizationService.updateById(one);

        if (name != null) {
            QueryWrapper<Organization> q = new QueryWrapper<>();
            q.eq("organization_name", name);
            Organization one1 = organizationService.getOne(q);
            one1.setNumber(one1.getNumber()-1);
            organizationService.updateById(one1);
        }

        Map<String, Object> map = new HashMap<>();
        if (b) {
            map.put("fd", "加入成功！(*^▽^*)");
            return map;
        }else {
            map.put("fd", "加入失败！o(╥﹏╥)o");
            return map;
        }
    }

    @ApiOperation("加入活动")
    @PostMapping("join/activity")
    public Map<String, Object> joinActivity(@RequestBody JoinDb join){
        boolean save = joinService.save(join);

        //构造条件
        QueryWrapper<JoinDb> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_name", join.getActivityName());
        long count = joinService.count(queryWrapper);

        QueryWrapper<Activity> query = new QueryWrapper<>();
        query.eq("activity_name", join.getActivityName());
        Activity one = activityService.getOne(query);
        one.setNumber(count);
        activityService.updateById(one);

        Map<String, Object> map = new HashMap<>();
        if (save) {
            map.put("message", "加入成功！(*^▽^*)");
            return map;
        }else {
            map.put("message", "加入失败！o(╥﹏╥)o");
            return map;
        }
    }

//    @ApiOperation("查看活动已报名人数")
//    @PostMapping("query")
//    public Map<String, Object> query(@RequestBody JoinDb join){
//        //构造条件
//        QueryWrapper<JoinDb> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("activity_name", join.getActivityName());
//        long count = joinService.count(queryWrapper);
//        Map<String, Object> map = new HashMap<>();
//
//        map.put("count", count);
//        return map;
//    }



    @ApiOperation("查看所有组织")
    @GetMapping("query/organization")
    public List<Organization> organization(){

        List<Organization> list = organizationService.list();
        return list;
    }

    @ApiOperation("查看活动参加情况")
    @GetMapping("query/join")
    public List<JoinDb> queryJoin(){

        List<JoinDb> list = joinService.list();
        return list;
    }
}
